/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.webbapps.cormac_susilo.bootstrap;

import edu.webbapps.cormac_susilo.core.Frame;
import edu.webbapps.cormac_susilo.core.FrameRegistry;
import edu.webbapps.cormac_susilo.core.Run;
import edu.webbapps.cormac_susilo.core.RunRegistry;
import edu.webbapps.cormac_susilo.core.User;
import edu.webbapps.cormac_susilo.core.UserRegistry;
import edu.webbapps.cormac_susilo.fit.CormacFitDecoder;
import java.io.InputStream;
import java.io.Serializable;

import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Singleton to generate bootstrap data for the project's memory database
 *
 * @author tor
 */
public class CormacBootstrap implements Serializable {

    private String DEFAULT_PU;
    private RunRegistry runRegistry;
    private UserRegistry userRegistry;
    private FrameRegistry frameRegistry;
    private static CormacBootstrap instance;

    public static CormacBootstrap getInstance() {
        if (instance == null) {
            instance = new CormacBootstrap();
        }
        return instance;
    }

    public void destroy() {
        frameRegistry.destroy();
        runRegistry.destroy();
        userRegistry.destroy();
        instance = null;
    }
    
    private CormacBootstrap() {
        DEFAULT_PU = "cormac_embedded";
        runRegistry = RunRegistry.newInstance(DEFAULT_PU);
        userRegistry = UserRegistry.newInstance(DEFAULT_PU);
        frameRegistry = FrameRegistry.newInstance(DEFAULT_PU);

        createStdData();
    }

    public RunRegistry getRunRegistry() {
        return this.runRegistry;
    }

    public UserRegistry getUserRegistry() {
        return this.userRegistry;
    }

    public FrameRegistry getFrameRegistry() {
        return this.frameRegistry;
    }

    private void createOneData(String username, String trackname, String filename) {
        User usr = userRegistry.findByName(username);
        if (usr == null) {
            usr = new User(username, "pass");
            userRegistry.add(usr);
            usr = userRegistry.find(usr.getId());
        }
        
        try {
            InputStream is = this.getClass().getClassLoader().getResourceAsStream(filename);
            
            CormacFitDecoder.loadDataIntoUser(usr, is, trackname, true);
            userRegistry.update(usr);
            
        } catch (Exception ex) {
            Logger.getLogger(CormacBootstrap.class.getName()).log(Level.SEVERE, "Conversion Error, User is not updated", ex);
        }
    }

    private void createStdData() {
        createOneData("Tor", "5 km","testfile.fit");
        createOneData("Tor", "5 km","testfile2.fit");
        createOneData("Tor","5 km","Tor2013-10-18-13-38-32.fit");
        createOneData("Simon","5 km", "Simon2013-09-30-16-09-22.fit");
        createOneData("Simon","5 km", "Simon2013-10-02-15-09-53.fit");
        //createOneData("Simon","5 km", "Simon2013-10-07-16-42-01.fit");
        //createOneData("Simon","10 km", "Simon2013-10-13-16-27-40.fit");
    }
}